package tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.Test;

import srcTA.InputData;

public class InputDataTests
{
    private class Context
    {
        public void CreateFile(File file) throws IOException
        {
            File parentDir = file.getParentFile();
            parentDir.mkdirs();
            file.createNewFile();
        }

        public void WriteToFile(File file, String data) throws IOException
        {
            if (!file.exists())
            {
                this.CreateFile(file);
            }

            BufferedWriter bufWriter = new BufferedWriter(new FileWriter(file, true));
            bufWriter.write(data);
            bufWriter.newLine();
            bufWriter.close();
        }
    }

    @Test
    public void ReadDataFromFile_ReadsData() throws IOException
    {
        // Arrange:
        Context context = new Context();
        File tempDir = TestHelpers.GetTempDirectory();
        File testDir = new File(tempDir, "testFolder");
        File testFile = new File(testDir, "testFile.txt");
        String testData = "1.5 \n\n77 \n\n29 \n\n9 \n\n0 97759.0 40 # \n\n2 418213.5 17 25 44 53 60 61 63 70 #";
        context.WriteToFile(testFile, testData);

        // Act:
        InputData inputData = InputData.ReadDataFromFile(testFile);

        // Assert:
        assertNotNull("inputData should not be null", inputData);
        assertEquals(1.5, inputData.TimeThreshold(), 0.01);
        assertEquals(77, inputData.NumRegions());
        assertEquals(29, inputData.NumBids());
        assertEquals(9, inputData.NumCompanies());
        assertEquals(2, inputData.Bids().size());
        assertEquals(2, inputData.Bids().get(1).Company());
        assertEquals(418213.5, inputData.Bids().get(1).Value(), 0.01);
        assertEquals(8, inputData.Bids().get(1).Regions().size());
    }

}
